var xhr = new XMLHttpRequest();
var $ = mdui.$;
var new1 = false;

function newtext(name1) {
            var url = name1;/*json文件url，本地的就写本地的位置，如果是服务器的就写服务器的路径*/
            var request = new XMLHttpRequest();
            request.open("get", url);/*设置请求方法与路径*/
            request.send(null);/*不发送数据到服务器*/
            request.onload = function () {/*XHR对象获取到返回信息后执行*/
                if (request.status == 200) {/*返回状态为200，即为数据获取成功*/
                    var json = JSON.parse(request.responseText);
					document.getElementById("what").innerHTML = json[0].title;
					document.getElementById("whattext").innerHTML = "";
                    for(var i=0;i<json.length;i++){
                       //document.getElementById("whattext").innerHTML = json[i].what;
					   var divID= document.getElementById("whattext");
                       divID.innerHTML+='<p><strong>'+json[i].what+'</strong></p>'
					   if(json[i].img){
						   var divID= document.getElementById("whattext");
                           divID.innerHTML+='<img src="'+json[i].img+'" height="'+json[i].height+'" width="'+json[i].width+'" />'
					   }
                    }
                }
            }
 
}

function loginuser(name1) {
	var user1 = document.getElementById("user1").value;
	var password1 = document.getElementById("password1").value;
            var url = name1;/*json文件url，本地的就写本地的位置，如果是服务器的就写服务器的路径*/
            var request = new XMLHttpRequest();
            request.open("get", url);/*设置请求方法与路径*/
            request.send(null);/*不发送数据到服务器*/
            request.onload = function () {/*XHR对象获取到返回信息后执行*/
                if (request.status == 200) {/*返回状态为200，即为数据获取成功*/
                    var json = JSON.parse(request.responseText);
                    for(var i=0;i<json.length;i++){
                       
                       if(json[i].username == user1){
						   if(json[i].password == password1){
							   setCookie("is_login","true",1);
							  setCookie("username",user1,1);
							  mdui.alert('登陆成功啦!欢迎回来'+getCookie("username"));
							   window.open('index.html','_self');
						   }
					   }
                    }
                }
            }
 
}

function setCookie(cname,cvalue,exdays){
	var d = new Date();
	d.setTime(d.getTime()+(exdays*24*60*60*1000));
	var expires = "expires="+d.toGMTString();
	document.cookie = cname+"="+cvalue+"; "+expires;
}

function getCookie(cname){
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i].trim();
		if (c.indexOf(name)==0) return c.substring(name.length,c.length);
	}
	return "";
}

function addServerUrlToJson() {
    var json_tem = [{"username":"test","password":"testn"}];
     document.write(JSON.stringify(json_tem));
} 

function checklog(){
	if(getCookie("is_login") == "true"){
		//mdui.alert('你的userid='+getCookie("username"));
		document.getElementById('logoff').innerHTML = '[测试]登出账号';
	}
}

$('#logoff').on('click', function(){
	if(getCookie("is_login") == "true"){
		setCookie("is_login","false",1);
		setCookie("username","null",1);
		mdui.alert('你的userid='+getCookie("username")+'退出登陆成功','退出登陆');
		setTimeout(function(){
		location.reload();
		},5 * 1000);
		
		
		
	}else{
		window.open('login.html','_self');
	}
});

        







